package com.wanxi.user.service.impl;


import com.wanxi.user.api.AddressFeign;
import com.wanxi.user.domain.dto.AddressDefaultDto;
import com.wanxi.user.domain.dto.AddressDto;
import com.wanxi.user.domain.entity.Address;
import com.wanxi.user.domain.result.UserResult;
import com.wanxi.user.mapper.AddressMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Author:Iny
 * @Data：2022/1/4 10:14
 */
@RestController
@RequestMapping("back/address")
public class AddressServiceImpl implements AddressFeign {

    @Resource
    private AddressMapper addressMapper;

    /**
     *
     * @param userId 用户id
     * @return
     */
    @ApiOperation(value="通过用户id查看收获地址")
    @GetMapping("findAddress")
    @Override
    public UserResult<List<Address>> findByUserId(long userId) {
        List<Address> addressList = addressMapper.findByUserId(userId);
        if (addressList!=null){
            return UserResult.success(addressList);
        }
        return UserResult.failed();
    }

    /**
     *
     * @param addressDto 收获地址信息
     * @return
     */
    @ApiOperation(value="通过用户id添加收获地址")
    @PostMapping("addAddress")
    @Override
    public UserResult<Long> addAddress(AddressDto addressDto) {
        //判断添加的收货地址默认值是不是1 是则把该用户的所有收货地址默认值全修改为不是默认地址
        if (addressDto.getIsDefault()==1){
            int update =  addressMapper.updateDefaultByUserIdAndDefault((int) addressDto.getUserId());
        }
        int address =addressMapper.addAddress(addressDto);
        long id = addressDto.getId();
        if (address>0){
            return UserResult.success(addressDto.getId());
        }
        return UserResult.failed();
    }

    @ApiOperation(value="通过收货地址id修改收获地址默认值")
    @PutMapping("updateAddressDefault")
    @Override
    public UserResult<Boolean> updateAddressDefault(AddressDefaultDto addressDefaultDto) {
        int userIdByAddress = addressMapper.findUserIdByAddress(addressDefaultDto.getId());
        int update =  addressMapper.updateDefaultByUserIdAndDefault(userIdByAddress);
        if (update>0){
            int i = addressMapper.updateDefaultByAddressId(addressDefaultDto);
            return UserResult.success(i>0);
        }
        return UserResult.failed(update>0);
    }

    @ApiOperation(value="通过收货地址id删除收获地址")
    @ApiImplicitParam(name = "id",value = "收获地址id",dataType = "long",required = true)
    @PutMapping("delAddress")
    @Override
    public UserResult<Boolean> delAddress(long id) {
        int del =  addressMapper.deleteAddress(id);
        if (del>0){
            return UserResult.success(del>0);
        }
        return UserResult.failed(del>0);
    }

    @ApiOperation(value="通过收货地址id修改收获地址")
    @GetMapping("findAddressById")
    @Override
    public UserResult<Address> findAddressById(long id) {

        Address address = addressMapper.findAddressById(id);
        if (address!=null){
            return UserResult.success(address);
        }
        return UserResult.failed();
    }


    @ApiOperation(value="通过收货地址id修改收获地址")
    @PutMapping("updateAddress")
    @Override
    public UserResult<Boolean> updateAddress(AddressDto addressDto) {

        int i = addressMapper.updateAddress(addressDto);
        if (i>0){
            int userIdByAddress = addressMapper.findUserIdByAddress(addressDto.getId());
            int update =  addressMapper.updateDefaultByUserIdAndDefault(userIdByAddress);
            AddressDefaultDto addressDefaultDto = AddressDefaultDto.builder()
                    .id(addressDto.getId())
                    .isDefault(addressDto.getIsDefault())
                    .build();
            int addressDefault = addressMapper.updateDefaultByAddressId(addressDefaultDto);
            return UserResult.success(i>0);
        }
        return UserResult.failed(i>0);
    }
}
